package com.stars.module.router.controller;

import com.stars.base.controller.BaseController;
import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.module.router.entity.StarRouter;
import com.stars.module.router.entity.StarRouterVo;
import com.stars.module.router.service.RouterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author : moon
 * @version : 1.0
 * @date : 2023/7/30
 * @description :
 */
@RestController
@RequestMapping("/router")
@Slf4j
public class RouterController extends BaseController {

    private final RouterService routerService;

    public RouterController(RouterService routerService) {
        this.routerService = routerService;
    }

    /**
     * 加载路由管理树
     * @author : moon
     * @date :  2023/9/4 11:23
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/manage/loadTree")
    public Result loadTree(){
        return Result.success(routerService.loadTree());
    }

    /**
     * 通过fid获取路由管理列表
     * @author : moon
     * @date :  2023/9/4 11:29
     * @param: dto 查询条件
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/manage/loadListByFid")
    public Result loadListByFid(@RequestBody CommonDTO dto) {
        return Result.success(routerService.loadListByFid(dto));
    }

    /**
     * 动态路由数据删除
     * @author : moon
     * @date :  2023/9/4 11:30
     * @param: id 动态路由id
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/manage/removeData")
    public Result removeData(String id){
        return routerService.removeData(id);
    }

    /**
     * 加载动态路由表单维护数据
     * @author : moon
     * @date :  2023/9/4 11:30
     * @param: id 动态路由id
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/manage/loadDataById")
    public Result loadDataById(String id) {
        return Result.success(routerService.loadDataById(id));
    }

    /**
     * 动态路由表单维护数据b
     * @author : moon
     * @date :  2023/9/4 17:21
     * @param: router
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/manage/save")
    public Result saveOrUpdateData(@RequestBody StarRouter router){
        return Result.result(routerService.saveOrUpdateData(router));
    }

    /**
     * 前端获取动态路由，后面应关联菜单，动态用户路由权限
     * @author : moon
     * @date :  2023/9/5 15:24
     * @return: com.stars.base.entity.Result
     */
    @RequestMapping("/getData")
    public Result getData () {
        List<StarRouterVo> list;
        try {
            list =  routerService.getUserRouter();
        } catch (Exception e) {
            log.error("动态路由获取异常", e);
            return Result.error(e.getMessage());
        }

        return Result.success(list);
    }
}
